package main

import (
	"log"
	"math/rand"
	"studygo/sp/a/zinx/ziface"
	zent "studygo/sp/a/zinx/znet"
	"time"
)

func init() {
	// 我们一般使用系统时间的不确定性来进行初始化
	rand.Seed(time.Now().UnixNano())
	log.SetPrefix("TRACE: ")
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}

/**
基于zinx 开发
*/
//自定义路由
type PingRouter struct {
	zent.BaseRouter
}

//Test PreHandle
func (r *PingRouter) PreHandler(request ziface.IRequest) {
	log.Println("Call Router PreHandle……")
	conn := request.GetConnection().GetTcpConnection()
	_, err := conn.Write([]byte("before ping...\n"))
	if err != nil {
		log.Println("call back before ping err", err)
	}

}
func (r *PingRouter) Handler(request ziface.IRequest) {
	log.Println("Call Router Handler……")
	conn := request.GetConnection().GetTcpConnection()
	_, err := conn.Write([]byte("ping ping...ping\n "))
	if err != nil {
		log.Println("call back ping ping err", err)
	}
}

func (r *PingRouter) PostHandler(request ziface.IRequest) {
	log.Println("Call Router PostHandler……")
	conn := request.GetConnection().GetTcpConnection()
	_, err := conn.Write([]byte("ping ping...after after\n"))
	if err != nil {
		log.Println("call back after ping err", err)
	}
}

func main() {
	//1 创建一个Server 句柄，使用zinx的API
	server := zent.NewServer("[zinc V0.1]")
	//2 给当前 添加router
	server.AddRouter(&PingRouter{})
	log.Println(server)

	server.Server()
}
